package com.yaowk.utils.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

public class GeneratorUtils {

    public static String getPath(String packageName, String basePath) {
        String packagePath = packageName.replaceAll("\\.", "/");
        URL url = Generator.class.getResource("/");
        String path = "";
        try {
            path = new File(url.getFile()).getParentFile().getParentFile().getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path + basePath + packagePath;
    }

    public static void write(File file, StringBuilder stringBuilder) {
        try {
            file.getParentFile().mkdirs();
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(stringBuilder.toString());
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String[] getGenericSuper(Class clazz) {
        String pk = clazz.getGenericSuperclass().getTypeName();
        pk = pk.substring(pk.lastIndexOf("<") + 1, pk.length() - 1);
        return parseClassType(pk);
    }

    public static String[] parseClassType(String classType) {
        String[] result = new String[2];
        result[1] = classType;
        result[0] = classType.substring(classType.lastIndexOf(".") + 1, classType.length());
        return result;
    }

    public static String setter(String fieldName) {
        return "set" + String.valueOf(fieldName.charAt(0)).toUpperCase() + fieldName.substring(1);
    }

    public static String getter(String fieldName) {
        return "get" + String.valueOf(fieldName.charAt(0)).toUpperCase() + fieldName.substring(1);
    }
}
